<?php
// +-----------------------+
// | 注释不留名，代码随便用 |
// +-----------------------+
namespace addons\manager;

use addons\Addons;
use think\Db;

class Bootstrap extends Addons
{
    /**
     * @var array manager 默认目录
     */
    public $defaultMenu = [
        'parent_title' => '系统配置',
        'title'     => '系统插件',
        'icon'      => 'layui-icon layui-icon-diamond',
        'url'       => '@addons/manager-plugs-index'
    ];

    protected $menus = [
        '系统插件' => '@addons/manager-plugs-index'
    ];
    public function install(){
        $this->addMenu();
    }
    public function uninstall()
    {
       //不支持自动卸载
    }

    /**
     * 添加到菜单栏
     */
    public function addMenu()
    {
        $parent = Db::name('system_menu')
            ->where('title', $this->defaultMenu['parent_title'])
            ->where('url', '#')
            ->find();
        $count =  Db::name('system_menu')
            ->where('pid', $parent['id'])
            ->where('title', $this->defaultMenu['title'])
            ->where('url', $this->defaultMenu['url'])
            ->count();
        if($count > 0) return ;
        if (empty($parent)) {
            abort(500, 'addons安装失败，找不到菜单栏【' . $this->parent_title . '】');
        }
        Db::name('system_menu')->insert([
            'pid' => $parent['id'],
            'icon' => $this->defaultMenu['icon'],
            'title' => $this->defaultMenu['title'],
            'target' => '_self',
            'status' => 1,
            'create_at' => date('Y-m-d H:i:s'),
            'url' => $this->defaultMenu['url'],
        ]);
    }
}